<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料编号">
              <a-input placeholder="请输入物料编号" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料类型">
                <j-search-select-tag placeholder="请选择物料类型" v-model="queryParam.materialTypeId" dict="mdm_material_type,material_type_name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="采购员">
                <j-search-select-tag placeholder="请选择采购员" v-model="queryParam.materialBuyerId" dict="sys_user,realname,username"  />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'material:add'" type="primary" icon="plus">新增</a-button>
      <a-button @click="copyAdd" v-has="'material:copyAdd'" type="primary" icon="copy">复制</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('mdm_material')"  v-has="'material:exp'" >导出</a-button>
      <a-upload name="file"  v-has="'material:imp'" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"  v-has="'material:delBatch'" ><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <div  style="float: right">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row>
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                      <a-col :span="6"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />自定义列</a>
          </a-popover>
        </div>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="materialProp_dictText" slot-scope="text, record">
          <yq-prop-capsule :value="text"/>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <span v-has="'material:edit'" >
            <a-divider type="vertical" />
            <a @click="handleEdit(record)">编辑</a>
          </span>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-has="'material:del'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mdm-material-modal ref="modalForm" @ok="modalFormOk"></mdm-material-modal>
    <mdm-material-modal-drawer ref="modalDetail" @ok="modalDetailOk"/>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MdmMaterialModal from './modules/MdmMaterialModal'
  import MdmMaterialModalDrawer from './modules/MdmMaterialModal.Style#Drawer'

  export default {
    name: 'MdmMaterialList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      MdmMaterialModal,
      MdmMaterialModalDrawer
    },
    data () {
      return {
        description: 'mdm_material管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物料编号',
            align:"center",
            width: 100,
            dataIndex: 'materialCode'
          },
          {
            title:'物料名称',
            align:"center",
            width: 160,
            dataIndex: 'materialName'
          },
          {
            title:'规格',
            align:"center",
            width: 160,
            dataIndex: 'materialSpec'
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'materialUnitCode_dictText'
          },
          {
            title:'物料类型',
            align:"center",
            width: 100,
            dataIndex: 'materialTypeId_dictText'
          },
          {
            title:'图片',
            align:"center",
            width: 100,
            dataIndex: 'materialPic',
            scopedSlots: {customRender: 'imgSlot'}
          },
          {
            title:'生产厂家',
            align:"center",
            width: 160,
            dataIndex: 'materialFactory'
          },
          {
            title:'采购员',
            align:"center",
            width: 100,
            dataIndex: 'materialBuyerId_dictText'
          },
          {
            title:'材质',
            align:"center",
            width: 100,
            dataIndex: 'materialMaterial'
          },
          {
            title:'图号',
            align:"center",
            width: 100,
            dataIndex: 'materialGraphNo'
          },
          {
            title:'属性组',
            align:"center",
            dataIndex: 'propGroupId_dictText'
          },
          {
            title:'属性',
            align:"center",
            width: 200,
            dataIndex: 'materialProp_dictText',
            scopedSlots: {customRender: 'materialProp_dictText'}
          },
          {
            title:'产地',
            align:"center",
            width: 100,
            dataIndex: 'materialOriginPlace'
          },
          {
            title:'品牌',
            align:"center",
            width: 100,
            dataIndex: 'materialBrand'
          },
          {
            title:'物料来源',
            align:"center",
            width: 100,
            dataIndex: 'materialSourceCode_dictText'
          },
          {
            title:'物料分类',
            align:"center",
            width: 100,
            dataIndex: 'materialClassId_dictText'
          },
          {
            title:'辅助单位',
            align:"center",
            dataIndex: 'auxiliaryUnitCode_dictText'
          },
          {
            title:'销售标志',
            align:"center",
            dataIndex: 'materialSaleFlag',
            customRender: (text) => (!text ? "否" : (text == [1,0][0] ? "是" : "否"))
          },
          {
            title:'启用标志',
            align:"center",
            dataIndex: 'enableFlag',
            customRender: (text) => (!text ? "否" : (text == [1,0][0] ? "是" : "否"))
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/mdm/material/list",
          delete: "/mdm/material/delete",
          deleteBatch: "/mdm/material/deleteBatch",
          exportXlsUrl: "/mdm/material/exportXls",
          importExcelUrl: "mdm/material/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
        isorter:{
          column: 'createTime,materialCode',
          order: 'desc',
        },
        ipagination:{
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30', '50', '100'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      copyAdd: function () {
        if (this.selectedRowKeys.length != 1 ) {
          this.$message.warning('请选择一条记录！');
          return;
        }
        let selecedtOne = this.selectionRows.find(row => row.id = this.selectedRowKeys[0])
        if(!selecedtOne){
          this.$message.warning('数据不存在！');
          return;
        }
        this.$refs.modalForm.copy(selecedtOne);
        this.$refs.modalForm.title = "复制";
        this.$refs.modalForm.disableSubmit = false;
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'materialCode',text:'物料编号',dictCode:''})
        fieldList.push({type:'string',value:'materialName',text:'物料名称',dictCode:''})
        fieldList.push({type:'sel_search',value:'materialTypeId',text:'物料类型ID',dictTable:'mdm_material_type', dictText:'material_type_name', dictCode:'id'})
        fieldList.push({type:'string',value:'materialSourceCode',text:'物料来源',dictCode:''})
        fieldList.push({type:'string',value:'materialUnitCode',text:'单位',dictTable:'mdm_unit', dictText:'unit_name', dictCode:'id'})
        fieldList.push({type:'string',value:'auxiliaryUnitCode',text:'辅助单位',dictCode:'unit'})
        fieldList.push({type:'string',value:'oldMaterialCode',text:'旧物料号',dictCode:''})
        fieldList.push({type:'string',value:'oldMaterialName',text:'旧物料名称',dictCode:''})
        fieldList.push({type:'string',value:'materialClassId',text:'分类ID',dictCode:''})
        fieldList.push({type:'string',value:'materialSaleFlag',text:'销售标志',dictCode:''})
        fieldList.push({type:'string',value:'inoutStrategyCode',text:'出入库策略',dictCode:''})
        fieldList.push({type:'string',value:'productionStockId',text:'生产仓库',dictCode:''})
        fieldList.push({type:'string',value:'buyStockId',text:'采购仓库',dictCode:''})
        fieldList.push({type:'string',value:'materialSpec',text:'规格',dictCode:''})
        fieldList.push({type:'string',value:'materialMaterial',text:'材质',dictCode:''})
        fieldList.push({type:'string',value:'materialGraphNo',text:'图号',dictCode:''})
        fieldList.push({type:'string',value:'materialOriginPlace',text:'产地',dictCode:''})
        fieldList.push({type:'string',value:'materialBrand',text:'品牌',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialWidth',text:'宽度',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialThickness',text:'厚度',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialLength',text:'长度',dictCode:''})
        fieldList.push({type:'string',value:'materialColor',text:'颜色',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialGrossWeight',text:'毛重',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialNetWeight',text:'净重',dictCode:''})
        fieldList.push({type:'string',value:'weighUnitCode',text:'重量单位',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialArea',text:'面积',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialVolume',text:'体积',dictCode:''})
        fieldList.push({type:'string',value:'volumeUnitCode',text:'体积单位',dictCode:''})
        fieldList.push({type:'string',value:'materialStatusCode',text:'物料状态',dictCode:''})
        fieldList.push({type:'date',value:'materialEffectiveDate',text:'生效日期'})
        fieldList.push({type:'date',value:'materialExpiryDate',text:'失效日期'})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'purDeptId',text:'采购组织',dictCode:''})
        fieldList.push({type:'string',value:'materialBuyerId',text:'采购员',dictCode:''})
        fieldList.push({type:'string',value:'materialBatchFlag',text:'批次标志',dictCode:''})
        fieldList.push({type:'string',value:'materialBuyFlag',text:'自动采购标志',dictCode:''})
        fieldList.push({type:'string',value:'materialAbcFlag',text:'ABC标识',dictCode:''})
        fieldList.push({type:'string',value:'materialPlannerId',text:'计划员',dictCode:''})
        fieldList.push({type:'string',value:'batchTypeId',text:'批量',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialMinimum',text:'最小批量',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialMaximum',text:'最大批量',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialFixed',text:'固定批量',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialSafeStock',text:'安全库存',dictCode:''})
        fieldList.push({type:'string',value:'materialBackFlag',text:'反冲标志',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'productionDays',text:'自制生产（天）',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'deliveryDays',text:'计划交货时间（天）',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'receiveDays',text:'收货处理时间（天）',dictCode:''})
        fieldList.push({type:'string',value:'unitConversionId',text:'单位换算定义ID',dictCode:''})
        fieldList.push({type:'string',value:'conversionMainUnit',text:'换算主单位',dictCode:''})
        fieldList.push({type:'string',value:'conversionSubUnit',text:'换算辅单位',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'conversionRate',text:'换算率',dictCode:''})
        fieldList.push({type:'string',value:'buyUnitCode',text:'默认采购单位',dictCode:''})
        fieldList.push({type:'string',value:'saleUnitCode',text:'默认销售单位',dictCode:''})
        fieldList.push({type:'string',value:'stockUnitCode',text:'默认库存单位',dictCode:''})
        fieldList.push({type:'string',value:'issueUnitCode',text:'默认发料单位',dictCode:''})
        fieldList.push({type:'string',value:'enableFlag',text:'启用标志',dictCode:''})
        this.superFieldList = fieldList
      },
      handleDetail(record){
        this.$refs.modalDetail.edit(record);
        this.$refs.modalDetail.title="详情";
        this.$refs.modalDetail.disableSubmit = true;
      },
      modalDetailOk() {
        this.loadData();
        this.onClearSelected()
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>